var tbsourcefactoryEvent={
    index_x:    3,//计算行高度比例 初始组件占了3个位置
    index_x_no: 1,//计算行号
    index_y:    1,//统计列个数
    rate_last:  0,//最后一个参数的高度或宽度
    //当增加列 保存当前的宽度
    x_width:    80,
    x_last_width: 80,
    table_source_data : function(){return JSON.parse(getLocalInfo("table_source_factory.html"))},
    table_source_arr : new Array(),//special_val
    table_source_arr_for_temp :function(){
        var table_source_arr_str = localStorage.getItem("temp_info_table_source_arr");
        var table_source_line_total =  localStorage.getItem("table_source_line_total");
        if(table_source_arr_str&&window.location.href.indexOf("template.html")>0){
            //alert(table_source_arr_str);
            this.table_source_arr = table_source_arr_str.split(",");
            this.index_y=this.table_source_arr.length;
            this.index_x_no=table_source_line_total;
            this.index_x=3+parseInt(this.index_x_no)-1;
            //计算下宽度初始化数据
            this.calculate_width_rate(80,this.index_y,"y");
            //alert(this.index_x);
            //alert(this.index_x_no);
        }
        //localStorage.removeItem("temp_info_table_source_arr");
        //localStorage.removeItem("table_source_line_total");
    },
    table_source_type_arr : new Array(),
    /*获取拼接字符串*/
    getStr:function(num,type){
        var str = "";
        if(num==2){
           str = '<div style="position: absolute;float: right;margin-left: 10px">'+
                '<input  type="button" value="删除列" onclick="tbsourcefactoryEvent.source_factory_delete_y(this)">'+
            '</div>'+
            '<div class="source-factory-comp-title" name="order_s_material_type">'+type+'</div>'+

            '<div  class="source-factory-comp-color" >色号</div>'+

            '<div class="source-factory-comp-descript" >颜色描述</div>'+
            '<input type="text" name="order_s_material_code" hidden>'+   
            '<div contenteditable="true" class="source-factory-comp-color-val" name="order_s_material_color">请输入或选择</div>'+

            '<div contenteditable="true" class="source-factory-comp-descript-val" name="order_s_material_description">请输入或选择'+'</div>';
        }
        if(num==3){
           str = '<div style="position: absolute;float: right;margin-left: 10px">'+
                '<input  type="button" value="删除列" onclick="tbsourcefactoryEvent.source_factory_delete_y(this)">'+
            '</div>'+
            '<div class="source-factory-comp-title" name="order_s_material_type">'+type+'</div>'+

            '<div  class="source-factory-comp-color" style="width:33%">色号</div>'+

            '<div class="source-factory-comp-descript" style="width:33%;float:left">颜色描述</div>'+
               
            '<div class="source-factory-comp-model" style="width:34%">'+
                '<div style="position: absolute;margin-top:15px">'+
                    '<input  type="button" value="删除规格" onclick="tbsourcefactoryEvent.source_factory_delete_model(this)">'+
                '</div>'+
            '规格型号</div>'+
               
            '<input type="text" name="order_s_material_code" hidden>'+
            '<div contenteditable="true" class="source-factory-comp-color-val" style="width:33%" name="order_s_material_color">请输入或选择</div>'+

            '<div contenteditable="true" class="source-factory-comp-descript-val" style="width:33%;float:left" name="order_s_material_description">请输入或选择'+'</div>'+
            
            '<div contenteditable="true" class="source-factory-comp-descript-val" style="width:34%" name="order_s_material_model">请输入或选择'+'</div>';
        }
        return str;
    },
    /*初始化第一个显示*/
    init:function(){
        this.table_source_arr[this.index_y-1]=this.table_source_data().special_val;
        this.table_source_type_arr[this.index_y-1]=this.table_source_data().text;
        $("[name='order_s_line_no']").text(this.index_y);
        $("#source_factory_comp_1").append(this.getStr(this.table_source_data().special_val,this.table_source_data().text));
        //contenteditable="true" binding event
        this.bindingGridForeditable();
     	//订单数和投料数绑定事件
        UtilForEveryAreaEvent.orderTotalFeedTotal_blur();
    },
    /*计算增加元素后的宽高值*/
    calculate_width_rate:function(source_rate,num,redirect){
        var remain_num = source_rate%num;
        //console.info(remain_num);
        if(remain_num==0){
            this.rate_last=source_rate/num;
        }else{
            this.rate_last=source_rate- parseInt(source_rate-remain_num)/num*(num-1);
        }
        //console.info(redirect);
        if(redirect=="y"){
            this.x_width=parseInt(source_rate-remain_num)/num;
            this.x_last_width=this.rate_last;
            //console.info("XXXXXXXXXXXXXXXXX"+":"+x_width+":"+x_last_width);
        }
        return parseInt(source_rate-remain_num)/num;
    },
    /*增加列*/
    source_factory_add_y:function(self){
        this.index_y++;
        var rate = this.calculate_width_rate(80,this.index_y,"y");
        //console.info(this.table_source_data());
        //console.info(JSON.parse(getLocalInfo("table_source_factory.html")));
        this.table_source_arr[this.index_y-1]=this.table_source_data().special_val;
        this.table_source_type_arr[this.index_y-1]=this.table_source_data().text;
        //console.info($(".source_factory_comp_father").find(".source-factory-comp").length+":"+$(".source-factory-comp").length)
        for(i=1;i< $(".source_factory_comp_father").find(".source-factory-comp").length-1;i++){
            //应该获取当前父节点下所有.source-factory-comp 不然修改了所有的
            $($(".source_factory_comp_father").find(".source-factory-comp")[i]).css("width",rate+"%");
        }

        $(".source_factory_comp_father").find(".source-factory-comp:last-child").before(
            '<div class="source-factory-comp" id="source_factory_comp_'+this.index_y+'" style="float: left;width: '+this.rate_last+'%">'+
                this.getStr(this.table_source_data().special_val,this.table_source_data().text)+
                /*'<div class="source-factory-comp-title">金属圈'+parseInt(Math.random() * (100) )+'<input  type="button" value="删除列" onclick="tbsourcefactoryEvent.source_factory_delete_y(this)"></div>'+

                '<div class="source-factory-comp-color">金属色号'+parseInt(Math.random() * (100) )+'</div>'+

                '<div class="source-factory-comp-descript">金属描述'+parseInt(Math.random() * (100) )+'</div>'+

                '<div class="source-factory-comp-color-val">GA1006'+parseInt(Math.random() * (100) )+'</div>'+

                '<div class="source-factory-comp-descript-val">黄色'+parseInt(Math.random() * (100) )+'</div>'+*/
            '</div>'

        )
        //重新调整尺寸 myid
        //console.info(this.index_y);
        //console.info($("[class^=source_factory_comp_father_]").length);
        $("[myid*='source_factory_comp_']").css("width",rate+"%");
        //计算从2开始有几行添加到行最后一个 并设置宽度rate_last
        var index_source_factory_comp_father = 2;
        for(i=0;i<$("[class^=source_factory_comp_father_]").length;i++){
            //每行添加一列
            /*console.info( $(".source_factory_comp_father_"+(index_source_factory_comp_father+i))
                .find("[myid='source_factory_comp_"+(index_y)+"']"))*/

            //error $(".source_factory_comp_father_"+(index_source_factory_comp_father+i))
            var str ="";
            if(this.table_source_arr[this.index_y-1]==2){
                str = '<div class="source-factory-comp" style="float: left;width: '+this.rate_last+'%" myid="source_factory_comp_'+(this.index_y+1)+'">'+
                '<input type="text" name="order_s_material_code" hidden>'+
                '<div contenteditable="true" name="order_s_material_color" class="source-factory-comp-color-val" style="height: 100%">请输入或选择</div>'+
                '<div contenteditable="true" name="order_s_material_description" class="source-factory-comp-descript-val" style="height: 100%">请输入或选择</div>'+
            '</div>';
            }else{
                str = '<div class="source-factory-comp" style="float: left;width: '+this.rate_last+'%" myid="source_factory_comp_'+(this.index_y+1)+'">'+
                '<input type="text" name="order_s_material_code" hidden>'+
                '<div contenteditable="true" name="order_s_material_color" class="source-factory-comp-color-val" style="height: 100%;width:33%">请输入或选择</div>'+
                    
                '<div contenteditable="true" name="order_s_material_description" class="source-factory-comp-descript-val" style="height: 100%;width:33%;float:left">请输入或选择</div>'+
                
                '<div contenteditable="true" name="order_s_material_model" class="source-factory-comp-descript-val" style="height: 100%;width:34%;float:left">请输入或选择</div>'+
            '</div>';
            }
            $($("[class^=source_factory_comp_father_]")[i]).find("[myid^='source_factory_comp_']:last").after(str);
        }
        //contenteditable="true" binding event
        this.bindingGridForeditable();
    },
    /*增加行*/
    source_factory_add_x:function (self){
        //console.info(this.table_source_arr);
        this.index_x_no++;
        this.index_x++;
        var rate = this.calculate_width_rate(100,this.index_x,"x")
        /*for(i=0;i< $(".source_factory_comp_father").length;i++){
            $($(".source_factory_comp_father")[i]).css("height",rate+"%");
        }*/
        for(i=0;i< $("[class^='source_factory_comp_father_']").length;i++){
            $($("[class^='source_factory_comp_father_']")[i]).css("height",rate+"%");
        }
        $($(".source_factory_comp_father")[0]).css("height",3*rate+"%");

        var str ='<div class="source_factory_comp_father_'+this.index_x_no+'" style="height: '+this.rate_last+'%" >'+
            '<div class="source-factory-comp" style="float: left;width: 5%">'+
                '<div name="order_s_line_no" class="source-factory-comp-serial-no-val" style="height: 100%">'+
                    ''+this.index_x_no+''+
                '<input  type="button" value="删除行" onclick="tbsourcefactoryEvent.source_factory_delete_x(this)">'+
                '</div>'+
            '</div>';
        //console.info(index_y+":"+x_width+":"+x_last_width);
        for(i=0;i<this.index_y;i++){
            //有几列 通过列数 从2 开始
            if(i==this.index_y-1){
                if(this.table_source_arr[i]==2){
                    str+='<div class="source-factory-comp" style="float: left;width: '+this.x_last_width+'%" myid="source_factory_comp_'+(2+i)+'">'+
                    '<input type="text" name="order_s_material_code" hidden>'+
                    '<div contenteditable="true" class="source-factory-comp-color-val" style="height: 100%" name="order_s_material_color">请输入或选择</div>'+

                    '<div contenteditable="true" class="source-factory-comp-descript-val" style="height: 100%" name="order_s_material_description">请输入或选择</div>'+
                    '</div>';
                }else{
                    str+='<div class="source-factory-comp" style="float: left;width: '+this.x_last_width+'%" myid="source_factory_comp_'+(2+i)+'">'+
                    '<input type="text" name="order_s_material_code" hidden>'+
                    '<div contenteditable="true" class="source-factory-comp-color-val" style="height: 100%;width:33%" name="order_s_material_color">请输入或选择</div>'+

                    '<div contenteditable="true" class="source-factory-comp-descript-val" style="height: 100%;width:33%;float:left" name="order_s_material_description">请输入或选择</div>'+
                        
                    '<div contenteditable="true" class="source-factory-comp-descript-val" style="height: 100%;width:34%" name="order_s_material_model">请输入或选择</div>'+
                    '</div>';
                }
            }else{
                if(this.table_source_arr[i]==2){
                    str+='<div class="source-factory-comp" style="float: left;width: '+this.x_width+'%" myid="source_factory_comp_'+(2+i)+'">'+
                    '<input type="text" name="order_s_material_code" hidden>'+
                    '<div contenteditable="true" class="source-factory-comp-color-val" name="order_s_material_color" style="height: 100%">请输入或选择</div>'+

                    '<div contenteditable="true" class="source-factory-comp-descript-val" name="order_s_material_description" style="height: 100%">请输入或选择</div>'+
                    '</div>';
                }else{
                    str+='<div class="source-factory-comp" style="float: left;width: '+this.x_width+'%" myid="source_factory_comp_'+(2+i)+'">'+
                    '<input type="text" name="order_s_material_code" hidden>'+
                    '<div contenteditable="true" class="source-factory-comp-color-val" style="height: 100%;width:33%" name="order_s_material_color">请输入或选择</div>'+

                    '<div contenteditable="true" class="source-factory-comp-descript-val" style="height: 100%;float:left;width:33%" name="order_s_material_description">请输入或选择</div>'+
                        
                    '<div contenteditable="true" class="source-factory-comp-descript-val" style="height: 100%;width:33%;width:34%" name="order_s_material_model">请输入或选择</div>'+
                    '</div>';
                }
                
            }
                
        }    

        str+='<div class="source-factory-comp" style="float: left;width: 15%">'+

                '<div contenteditable="true" name="order_s_order_total" class="source-factory-comp-color-val" style="height: 100%">0</div>'+

                '<div contenteditable="true" name="order_s_feed_total" class="source-factory-comp-descript-val" style="height: 100%">0</div>'+
            '</div>'+    
            '</div>';
        //console.info(str);
        $("#parent").append(str);
        //contenteditable="true" binding event
        this.bindingGridForeditable();
        
        $("[name='order_s_order_total']").unbind("blur");
        $("[name='order_s_order_total']").bind("blur",function(){$(this).text($(this).text().replace(/[^\d]/g,''))})
        $("[name='order_s_feed_total']").unbind("blur");
        $("[name='order_s_feed_total']").bind("blur",function(){$(this).text($(this).text().replace(/[^\d]/g,''))})
        return false;

    },    
    /*删除列*/
    source_factory_delete_y:function(self){
        //console.info($(self).parent());
        
        if($(self).parent().parent().siblings().length==2){
            show_error_dialog("操作错误:列项请保留一列");
            return;
        };
        $(self).parent().parent().remove();
        this.index_y--;
        var rate = this.calculate_width_rate(80,this.index_y,"y");
        for(i=1;i< $(".source_factory_comp_father").find(".source-factory-comp").length-1;i++){
            $($(".source_factory_comp_father").find(".source-factory-comp")[i]).css("width",rate+"%");
            //最后一个宽度
            if(i==$(".source_factory_comp_father").find(".source-factory-comp").length-2){
            	$($(".source_factory_comp_father").find(".source-factory-comp")[i]).css("width",this.x_last_width+"%");
            }
        }

        //更新序号2及之后的行 并删除对应行该列的数据
        var myid = $(self).parent().parent().attr("id");
        //alert(myid);
        var index = parseInt(myid.substr(myid.length-1,myid.length))+1;
        this.table_source_arr.splice(index-2,1);
        this.table_source_type_arr.splice(index-2,1);
        //console.info(this.table_source_arr);
        /*alert("[myid='source_factory_comp_"+index+"']");
        alert($("[myid='source_factory_comp_"+index+"']").length);*/
        $("[myid='source_factory_comp_"+index+"']").remove();

        //修正列序号问题及对应列序号
        for(i=0;i<$("[id^='source_factory_comp_']").length;i++){
            var myid = $($("[id^='source_factory_comp_']")[i]).attr("id");
            var index = parseInt(myid.substr(myid.length-1,myid.length))+1;
            $("[myid='source_factory_comp_"+index+"']").attr("myid","source_factory_comp_"+(i+2));
            $($("[id^='source_factory_comp_']")[i]).attr("id","source_factory_comp_"+(i+1));
        }
        //重新调整尺寸
        $("[myid^='source_factory_comp_']").css("width",rate+"%");
        //最后一个宽度
        $("[class^='source_factory_comp_father_']").find("[myid='source_factory_comp_"+(this.index_y+1)+"']").css("width",this.x_last_width+"%");
        
        //重新显示左边侧边栏节点 好恶心
        $(".left").find("#left_source_factory_content").find("a:contains('"+$(self).parent().next().text()+"')").each(function(){
            if($(this).text() == $(self).parent().next().text()){
                $(this).parent().show();
            }
        });
    },
    /*删除行*/
    source_factory_delete_x:function(self){
        //console.info($(self).parent());
        this.index_x_no--;
        this.index_x--;
        $(self).parent().parent().parent().remove();
        //重新调整尺寸 高度
        var rate = this.calculate_width_rate(100,this.index_x,"x");
        /*for(i=0;i< $(".source_factory_comp_father").length;i++){
            $($(".source_factory_comp_father")[i]).css("height",rate+"%");
        }*/
        this.index_x_no=1;
        for(i=0;i< $("[class^='source_factory_comp_father_']").length;i++){
            $($("[class^='source_factory_comp_father_']")[i]).css("height",rate+"%");
            //修正序号问题
            this.index_x_no++
            $($("[class^='source_factory_comp_father_']")[i]).find(".source-factory-comp-serial-no-val").html(this.index_x_no+'<input  type="button" value="删除行" onclick="tbsourcefactoryEvent.source_factory_delete_x(this)">');
            $($("[class^='source_factory_comp_father_']")[i]).attr("class","source_factory_comp_father_"+this.index_x_no);
        }
        
        $($(".source_factory_comp_father")[0]).css("height",3*rate+"%");
    },
    /*给 contenteditable="true" */
    bindingGridForeditable:function(self){
        //排除订单数 投料数
        //.not("[name='order_s_feed_total']").not("[name='order_s_order_total']")
    	//原材料仓contenteditable='true'
    	//判断新功能模版
    	if($(".source_factory_colresizeable").length>0){
    		return;
    	}
    	if($(".source_factory_colresizeable_for_two").length>0){
    		return;
    	}
        $("[id^='draggable_source_factory_content_']").find("[contenteditable='true']").not("[name='order_s_feed_total']").not("[name='order_s_order_total']").unbind("click.editable");
        
        $("[id^='draggable_source_factory_content_']").find("[contenteditable='true']").not("[name='order_s_feed_total']").not("[name='order_s_order_total']").bind("click.editable",function(e){
            var material_source_type = "";
            //判断是否是序号2开始以上的
            var myid = $($(this).parent()).attr("myid");
            var father=$($(this).parent().parent()).attr("class");
            if(myid!=undefined&&myid!="undefined"){
                myid ="myid"+":"+myid
                
                var index = parseInt($($(this).parent()).attr("myid").split("source_factory_comp_")[1]);
                material_source_type = tbsourcefactoryEvent.table_source_type_arr[index-2];
            }else{
                myid = "id"+":"+($(this).parent()).attr("id");
                
                var index = parseInt($($(this).parent()).attr("id").split("source_factory_comp_")[1]);
                material_source_type = tbsourcefactoryEvent.table_source_type_arr[index-1];
            }
            //console.info("material_source_type:"+material_source_type);
            console.info($(this).attr("name"));
            $('div#easyui_dialog_source').dialog({
                modal: true,
                title: '请选择',
                width: "550",
                height: "400",
                closed: false,
                border:false,
                cache: false,
                resizable: true,
                content:"<iframe frameborder='0' scrolling='no' style='width:99%;height:97%;' src='source_factory_dialog.html?material_source_type="+material_source_type+"&dom="+myid+"&ev="+$(this).attr("name")+"&father="+father+"'></iframe>",
                onClose : function() {  
                   tbsourcefactoryEvent.destroydialog();
                }
            });
        });
    },
    destroydialog:function(){
        $("div#easyui_dialog_source").dialog('destroy');  
        $("body").append("<div id='easyui_dialog_source'></div>");
    },
    source_factory_delete_model:function(self){
        //删除该列的规格型号
        //调整序号1尺寸
        //console.info($(self).parent().parent().parent().attr("id"));
        var len = $(self).parent().parent().parent().find("[name='order_s_material_model']").prevAll().not("[name='order_s_material_type']").length;
        $(self).parent().parent().parent().find("[name='order_s_material_model']").prevAll().not("[name='order_s_material_type']").each(function(i,elem){
            console.info(i);
            //去除删除列按钮
            if(i<len-1){
                $(this).css("width","50%");   
            }
        })
        
        
        $(self).parent().parent().parent().find("[name='order_s_material_model']").siblings("[contenteditable='true']").css("width","50%");
        //修改数组原 3 --> 2
        var index=                     $(self).parent().parent().parent().attr("id").substring($(self).parent().parent().parent().attr("id").length-1);
        this.table_source_arr[index-1] = this.table_source_arr[index-1]-1;
        //console.info($(self).val());
        
        $(self).parent().parent().parent().find("[name='order_s_material_model']").remove();
        $(self).parent().parent().remove();
        
        //删除序号2每行该列的规格型号
        //调整尺寸
        $("[class^='source_factory_comp_father_']").each(function(i,elem){
            $(this).find("[myid='source_factory_comp_"+(parseInt(index)+1)+"']").find("[contenteditable='true']").css("width","50%");
            
            $(this).find("[myid='source_factory_comp_"+(parseInt(index)+1)+"']").find("[name='order_s_material_model']").remove();
        })
        
    },initReloadData:function(){
    	//组件没有的时候初始化数据
    	this.index_x=3;//计算行高度比例 初始组件占了3个位置
    	this.index_x_no=1;//计算行号
    	this.index_y=1;//统计列个数
    	this.rate_last=0;//最后一个参数的高度或宽度
        //当增加列 保存当前的宽度
    	this.x_width=80;
    	this.x_last_width=80;
    	this.table_source_arr=new Array();//special_val
    	this.table_source_type_arr=new Array();//special_val
    }
}
/*事件版定*/
tbsourcefactoryEvent.bindingGridForeditable();
/*为了模版*/
tbsourcefactoryEvent.table_source_arr_for_temp();



